// web socket
const connect = document.querySelector('#connectId')
const send = document.querySelector('#sendId')
const close = document.querySelector('#closeId')

let ws = null
connect.addEventListener('click', function(){
    ws = new WebSocket('ws://localhost:12139');
    ws.onopen = () => {
        console.log('连接服务端ws成功')
    }
    ws.onclose = () => {
        console.log('连接服务端ws失败 / 关闭')
    }
    ws.onmessage = msg => {
        console.log(`onmessage：${msg.data}`)
    }
})

send.addEventListener('click', function(){
    if(!ws){
        console.warn('ws is null')
        return
    }
    ws.send('aaa')
})

close.addEventListener('click', function(){
    if(!ws){
        console.warn('ws is null')
        return
    }
    ws.close()
})